import notification from 'ant-design-vue/es/notification'
import axios from 'axios'
import qs from 'qs'

// 创建 axios 实例
const service = axios.create({
  baseURL: '/mom', //贝伦思打包地址
  timeout: 999999999, // 请求超时时间
})

const err = (error) => {
  if (error.response) {
    if (error.response.status === 404) {
      notification.error({
        message: '资源丢失',
        description: '资源丢失,请联系管理员',
      })
    }
  } else {
    Promise.reject(error)
  }
}
// request interceptor
service.interceptors.request.use((config) => {
  // if (config.baseUrl) {
  //   config.baseURL = config.baseUrl
  // }
  // if (!config.headers['Authorization']) {
  //   config.headers['Authorization'] =
  //     'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJNRVMiLCJzdWIiOiJhZG1pbiIsInJvbGVzIjpbImFkbWluIiwiMDAyIiwiUmVzaWduZWQiXSwiaXNzIjoibmp1dG8tc2VydmVyIiwibmFtZSI6IumZhuS4peW9uyIsImV4cCI6NDgyMjc5MjA0OCwiaWF0IjoxNjY5MTkxNDQ4LCJqdGkiOiJjN2E4NTY2Nzc0NWY0MWMzYWE1YTk5MDVjOTJjYmVmYiJ9.3h97bQ062GwVUz73NDp-LAm41CTxJkJZcg0-pkRAqgY'
  // }
  if (!config.headers['Content-Type']) {
    config.headers['Content-Type'] =
      'application/x-www-form-urlencoded;charset=UTF-8'
  }
  if (
    config.data &&
    typeof config.data !== 'string' &&
    config.headers['Content-Type'] !== 'multipart/form-data'
  ) {
    config.data = qs.stringify(config.data)
  }
  config.headers['X-Requested-with'] = 'XMLHttpRequest'
  return config
}, err)

// response interceptor
service.interceptors.response.use((response) => {
  return response.data
}, err)

export { service as axios }
